// 继承的实现
function extend_usemid(Child, Parent)
{
	var F = function()
	{
	};
	F.prototype = Parent.prototype;
	Child.prototype = new F();
	Child.prototype.constructor = Child;
}

function Object_Base(a_id)
{
	// id
	this.id = a_id;
	this.object_type = this.constructor.name;
}

/**
 * 初始化静态信息
 */
Object_Base.prototype.init = function()
{
}

/**
 * 取消可视 action 目前只有0 就是可视与否
 */
Object_Base.prototype.setUnsee = function()
{
	if (this["datas"] !== undefined)
	{
		for ( var i = 0; i < this["datas"].length; i++)
		{
			this["datas"][i].updateArray("view_state", 0, 1);
		}
	}
	else
	{
		this.updateArray("view_state", 0, 1);
	}
}

/**
 * 取消可视 action 目前只有0 就是可视与否
 */
Object_Base.prototype.setUnclick = function()
{
	if (this["datas"] !== undefined)
	{
		for ( var i = 0; i < this["datas"].length; i++)
		{
			this["datas"][i].updateArray("op_state", 0, 1);
		}
	}
	else
	{
		this.updateArray("op_state", 0, 1);
	}
}

/**
 * 修改数组方法
 */
Object_Base.prototype.update = function(target, action, value)
{
	if (this["datas"] !== undefined)
	{
		for ( var i in this["datas"])
		{
			if (action === "all")
			{
				for ( var i = 0; i < this["datas"].length; i++)
				{
					for ( var k = 0; k < this["datas"][i][target].length; k++)
					{
						this["datas"][i][target][k] = value;
					}
				}
			}
			else
			{
				for ( var i = 0; i < this["datas"].length; i++)
				{
					this["datas"][i][target][action] = value;
				}
			}
		}
	}
	else
	{
		if (action === "all")
		{
			for ( var k = 0; k < this["datas"][i][target].length; k++)
			{
				this[target][k] = value;
			}
		}
		else
		{
			this[target][action] = value;
		}
	}

}

/**
 * 初始化静态信息
 */
Object_Base.prototype.init = function()
{
}

/**
 * 对象每个属性的赋值(复制),从协议中
 */
Object_Base.prototype.parseFromPro = function(a_pro)
{
	var _order = null;
	for ( var attr in a_pro)
	{
		if (a_pro !== null && a_pro !== undefined
				&& typeof (this[attr]) !== "function"
				&& a_pro[attr] !== undefined && a_pro[attr] !== null)
		{
			// 该属性是数组
			if (a_pro[attr] instanceof Array)
			{
				for ( var each in a_pro[attr])
				{
					if (a_pro[attr][each] !== undefined
							&& a_pro[attr][each] !== null
							&& this[attr] !== undefined && this[attr] !== null)
					{
						// 本地的对象还未有信息，数据来了执行添加操作
						if (this[attr].length === 0 || _order === "add")
						{
							// alert("new " + a_pro[attr][each]["object_type"]
							// + "();");
							// 此处new一个实例，是Can里面的datas里的每个data
							var _obj = eval("new "
									+ a_pro[attr][each]["object_type"] + "();");
							_obj.parseFromPro(a_pro[attr][each]);

							// init
							if (typeof (_obj["init"]) === "function")
							{
								// 不init内部元素
								// _obj.init();
							}

							_order = "add";
							// 此处push每个data进Can里
							this[attr].push(_obj);
						}
						// 本地的对象已经有信息 要判断是否有重复的，重复的则做更新操作
						else if (this[attr].length > 0)
						{
							// 检查是否已存在
							var _obj = this.checkExist(this[attr], "id",
									a_pro[attr][each]["id"]);
							alert(_obj);
							if (_obj === null)
							{
								var _addObj = eval("new "
										+ a_pro[attr][each]["object_type"]
										+ "();");
								_addObj.parseFromPro(a_pro[attr][each]);
								// 此处push每个data进Can里
								this[attr].push(_addObj);
							}
							else
							{
								_obj.parseFromPro(a_pro[attr][each]);
							}

							// for ( var thisEach in this[attr])
							// {
							// if (this[attr][thisEach]["id"] === undefined
							// || a_pro[attr][each]["id"] ===
							// this[attr][thisEach]["id"])
							// {
							// alert(this[attr].length + " "
							// + a_pro[attr].length + " "
							// + this[attr][thisEach]["id"] + " "
							// + a_pro[attr][each]["id"]);
							// this[attr][thisEach]
							// .parseFromPro(a_pro[attr][each]);
							// break;
							// }
							// else
							// {
							//
							// }
							// }
						}
						else
						{

						}
					}
				}
			}
			// 不是数组
			else if (typeof (a_pro[attr]) === "object"
					&& a_pro[attr].constructor === Object)
			{
				this[attr].parseFromPro(a_pro[attr]);
				// init
				if (typeof (this[attr]["init"]) === "function")
				{
					// 不init内部元素
					// eval("this[\"" + attr + "\"].init();");
				}
			}
			else
			{
				this[attr] = a_pro[attr];
			}
		}
	}
}

/**
 * 判断一个数组里面，匹配field的值，看是否有已存在的元素
 */
Object_Base.prototype.checkExist = function(array, field, matchvalue)
{
	var _result = null;
	if (array !== undefined && array !== null && field !== undefined
			&& field !== null)
	{
		for ( var i in array)
		{
			if (array[i] !== undefined && array[i] !== null)
			{
				if (array[i][field] === matchvalue)
				{
					_result = array[i];
					break;
				}
			}
		}
	}
	return _result;
}

function Object_Fact(a_id)
{
	Object_Base.apply(this, arguments);
}
extend_usemid(Object_Fact, Object_Base);

function Object_Util(a_id)
{
	Object_Base.apply(this, arguments);
}
extend_usemid(Object_Util, Object_Base);

function Object_Attr(a_id)
{
	Object_Base.apply(this, arguments);
}
extend_usemid(Object_Attr, Object_Base);
